home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / STD.H < prev    next >
C/C++ Source or Header  |  1993-05-13  |  16KB  |  399 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* std.h */
  20. /* Standard definitions for Aladdin Enterprises code */
  21.  
  22. #ifndef std_INCLUDED
  23. #  define std_INCLUDED
  24.  
  25. /* Include the architecture definitions. */
  26. #include "arch.h"
  27. #define arch_ints_are_short (arch_sizeof_int == arch_sizeof_short)
  28.  
  29. /*
  30.  * Here we deal with the vagaries of various C compilers.  We assume that:
  31.  *    ANSI-standard Unix compilers define __STDC__.
  32.  *    Borland Turbo C and Turbo C++ define __MSDOS__ and __TURBOC__.
  33.  *    Borland C++ defines __BORLANDC__, __MSDOS__, and __TURBOC__.
  34.  *    Microsoft C/C++ defines _MSC_VER and _MSDOS.
  35.  *    Watcom C defines __WATCOMC__ and MSDOS.
  36.  *
  37.  * We arrange to define __MSDOS__ on all the MS-DOS platforms.
  38.  */
  39. #if (defined(MSDOS) || defined(_MSDOS)) && !defined(__MSDOS__)
  40. #  define __MSDOS__
  41. #endif
  42. /*
  43.  * Also, not used much here, but used in other header files, we assume:
  44.  *    Unix System V environments define USG or SYSV.
  45.  *      (GNU software uses the former, non-GNU tends to use the latter.)
  46.  *    The SCO ODT compiler defines M_SYSV and M_SYS3.
  47.  *    VMS systems define VMS.
  48.  *      (The VMS C compiler handles prototypes and const, but does not
  49.  *      define __STDC__.)
  50.  *    bsd 4.2 or 4.3 systems define BSD4_2.
  51.  *    POSIX-compliant environments define _POSIX_SOURCE.
  52.  *    Motorola 88K BCS/OCS systems defined m88k.
  53.  *    OSF/1 compilers define __osf__ or __OSF__.
  54.  *
  55.  * We make fairly heroic efforts to confine all uses of these flags to
  56.  * header files, and never to use them in code.
  57.  */
  58. #if defined(__osf__) && !defined(__OSF__)
  59. #  define __OSF__ /* */
  60. #endif
  61. #if defined(M_SYSV) && !defined(SYSV)
  62. #  define SYSV /* */
  63. #endif
  64. #if defined(M_SYS3) && !defined(__SVR3)
  65. #  define __SVR3 /* */
  66. #endif
  67.  
  68. #if defined(__STDC__) || defined(__MSDOS__) || defined(__convex__) || defined(VMS)
  69. # if defined(M_UNIX) || defined(__GNUC__) || !defined(M_XENIX) /* SCO Xenix cc is broken */
  70. #  define __PROTOTYPES__ /* */
  71. # endif
  72. #endif
  73.  
  74. /* Recognize USG and SYSV as synonyms.  GNU software uses the former, */
  75. /* non-GNU tends to use the latter.  We use the latter. */
  76. #ifdef USG
  77. # ifndef SYSV
  78. #  define SYSV /* */
  79. # endif
  80. #endif
  81.  
  82. /* Define dummy values for __FILE__ and __LINE__ if the compiler */
  83. /* doesn't provide these.  Note that places that use __FILE__ */
  84. /* must check explicitly for a null pointer. */
  85. #ifndef __FILE__
  86. #  define __FILE__ NULL
  87. #endif
  88. #ifndef __LINE__
  89. #  define __LINE__ 0
  90. #endif
  91.  
  92. /* Disable 'const' and 'volatile' if the compiler can't handle them. */
  93. #ifndef __PROTOTYPES__
  94. #  undef const
  95. #  define const /* */
  96. #  undef volatile
  97. #  define volatile /* */
  98. #endif
  99.  
  100. /*
  101.  * The SVR4.2 C compiler incorrectly considers the result of << and >>
  102.  * to be unsigned if the left operand is signed and the right operand is
  103.  * unsigned.  We believe this only causes trouble in Ghostscript code when
  104.  * the right operand is a sizeof(...), which is unsigned for this compiler.
  105.  * Therefore, we replace the relevant uses of sizeof with size_of:
  106.  */
  107. #define size_of(x) ((int)(sizeof(x)))
  108.  
  109. /* Disable MS-DOS specialized pointer types on non-MS-DOS systems. */
  110. /* Watcom C defines near, far, and huge as macros, so we must undef them. */
  111. /* far_data is used for static data that must get its own segment. */
  112. /* This is supported in Borland C++, but none of the others. */
  113. #undef far_data
  114. #ifdef __TURBOC__
  115. #  ifdef __BORLANDC__
  116. #    define far_data far
  117. #  else
  118. #    define far_data /* */
  119. #  endif
  120. #else
  121. #  undef near
  122. #  define near /* */
  123. #  undef far
  124. #  define far /* */
  125. #  define far_data /* */
  126. #  undef huge
  127. #  define huge /* */
  128. #  define _cs /* */
  129. #  define _ds /* */
  130. /* _es is never safe to use */
  131. #  define _ss /* */
  132. #endif
  133.  
  134. /* Define a couple of useful language extensions. */
  135. /* Get the size of a statically declared array. */
  136. #define countof(a) (sizeof(a) / sizeof((a)[0]))
  137. #define count_of(a) (size_of(a) / size_of((a)[0]))
  138. /* Get the offset of a structure member. */
  139. /* Amazingly enough, this appears to work on all compilers */
  140. /* (except for one broken MIPS compiler). */
  141. #define offset_of(type, memb) ((int) &((type *) 0)->memb)
  142.  
  143. /* Define short names for the unsigned types. */
  144. typedef unsigned char byte;
  145. typedef unsigned char uchar;
  146. typedef unsigned short ushort;
  147. typedef unsigned int uint;
  148. typedef unsigned long ulong;
  149.  
  150. /* Since sys/types.h often defines one or more of these (depending on */
  151. /* the platform), we have to take steps to prevent name clashes. */
  152. /*** NOTE: This requires that you include std.h *before* any other ***/
  153. /*** header file that includes sys/types.h. ***/
  154. #define uchar uchar_
  155. #define uint uint_
  156. #define ushort ushort_
  157. #define ulong ulong_
  158. #include <sys/types.h>
  159. #undef uchar
  160. #undef uint
  161. #undef ushort
  162. #undef ulong
  163.  
  164. /* Minimum and maximum values for the signed types. */
  165. #define min_signed(type) (-(type)1 << (size_of(type) * 8 - 1))
  166. #define min_short min_signed(short)
  167. #define max_short (~min_short)
  168. #define min_int min_signed(int)
  169. #define max_int (~min_int)
  170. #define min_long min_signed(long)
  171. #define max_long (~min_long)
  172.  
  173. /*
  174.  * The maximum values for the unsigned types are defined in arch.h,
  175.  * because so many compilers handle unsigned constants wrong.
  176.  * In particular, most of the DEC VMS compilers incorrectly sign-extend
  177.  * short unsigned constants (but not short unsigned variables) when
  178.  * widening them to longs.  We program around this on a case-by-case basis.
  179.  * Some compilers don't truncate constants when they are cast down.
  180.  * The UTek compiler does special weird things of its own.
  181.  * All the rest (including gcc on all platforms) do the right thing.
  182.  */
  183. #define max_uchar arch_max_uchar
  184. #define max_ushort arch_max_ushort
  185. #define max_uint arch_max_uint
  186. #define max_ulong arch_max_ulong
  187.  
  188. /* Define a reliable arithmetic right shift. */
  189. /* Must use arith_rshift_1 for a shift by a literal 1. */
  190. #define arith_rshift_slow(x,n) ((x) < 0 ? ~(~(x) >> (n)) : (x) >> (n))
  191. #if arch_arith_rshift == 2
  192. #  define arith_rshift(x,n) ((x) >> (n))
  193. #  define arith_rshift_1(x) ((x) >> 1)
  194. #else
  195. #if arch_arith_rshift == 1        /* OK except for n=1 */
  196. #  define arith_rshift(x,n) ((x) >> (n))
  197. #  define arith_rshift_1(x) arith_rshift_slow(x,1)
  198. #else
  199. #  define arith_rshift(x,n) arith_rshift_slow(x,n)
  200. #  define arith_rshift_1(x) arith_rshift_slow(x,1)
  201. #endif
  202. #endif
  203.  
  204. /* The type to be used for comparing pointers for order (<, >=, etc.). */
  205. /* Turbo C large model doesn't compare pointers correctly */
  206. /* if they have different segment parts. */
  207. #ifdef __TURBOC__
  208. typedef unsigned long ptr_ord_t;
  209. #else
  210. typedef char *ptr_ord_t;
  211. #endif
  212. /* Define all the pointer comparison operations. */
  213. #define _ptr_cmp(p1, rel, p2)  ((ptr_ord_t)(p1) rel (ptr_ord_t)(p2))
  214. #define ptr_le(p1, p2) _ptr_cmp(p1, <=, p2)
  215. #define ptr_lt(p1, p2) _ptr_cmp(p1, <, p2)
  216. #define ptr_ge(p1, p2) _ptr_cmp(p1, >=, p2)
  217. #define ptr_gt(p1, p2) _ptr_cmp(p1, >, p2)
  218. #define ptr_between(ptr, lo, hi)\
  219.   (ptr_ge(ptr, lo) && ptr_lt(ptr, hi))
  220.  
  221. /* Define max and min, but make sure to use the identical definition */
  222. /* to the one that all the compilers seem to have.... */
  223. #ifndef min
  224. #  define min(a, b) (((a) < (b)) ? (a) : (b))
  225. #endif
  226. #ifndef max
  227. #  define max(a, b) (((a) > (b)) ? (a) : (b))
  228. #endif
  229.  
  230. /* Define a standard way to round values to a (constant) modulus. */
  231. #define round_down(value, modulus)\
  232.   ( (modulus) & ((modulus) - 1) ?    /* not a power of 2 */\
  233.     (value) - (value) % (modulus) :\
  234.     (value) & -(modulus) )
  235. #define round_up(value, modulus)\
  236.   ( (modulus) & ((modulus) - 1) ?    /* not a power of 2 */\
  237.     ((value) + ((modulus) - 1)) / (modulus) * (modulus) :\
  238.     ((value) + ((modulus) - 1)) & -(modulus) )
  239.  
  240. /* VMS doesn't have the unlink system call.  Use delete instead. */
  241. #ifdef VMS
  242. #  define unlink(fname) delete(fname)
  243. #endif
  244.  
  245. /*
  246.  * In pre-ANSI C, float parameters get converted to double.
  247.  * However, if we pass a float to a function that has been declared
  248.  * with a prototype, and the parameter has been declared as float,
  249.  * the ANSI standard specifies that the parameter is left as float.
  250.  * To avoid problems caused by missing prototypes,
  251.  * we declare almost all float parameters as double.
  252.  */
  253. typedef double floatp;
  254.  
  255. /* If we are debugging, make all static variables and procedures public */
  256. /* so they get passed through the linker. */
  257. #ifdef NOPRIVATE
  258. # define private /* */
  259. #else
  260. # define private static
  261. #endif
  262.  
  263. /*
  264.  * Macros for argument templates.  ANSI C has these, as does Turbo C,
  265.  * but older pcc-derived (K&R) Unix compilers don't.  The syntax is
  266.  *    resulttype func(Pn(arg1, ..., argn));
  267.  */
  268.  
  269. #ifdef __PROTOTYPES__
  270. # define P0() void
  271. # define P1(t1) t1
  272. # define P2(t1,t2) t1,t2
  273. # define P3(t1,t2,t3) t1,t2,t3
  274. # define P4(t1,t2,t3,t4) t1,t2,t3,t4
  275. # define P5(t1,t2,t3,t4,t5) t1,t2,t3,t4,t5
  276. # define P6(t1,t2,t3,t4,t5,t6) t1,t2,t3,t4,t5,t6
  277. # define P7(t1,t2,t3,t4,t5,t6,t7) t1,t2,t3,t4,t5,t6,t7
  278. # define P8(t1,t2,t3,t4,t5,t6,t7,t8) t1,t2,t3,t4,t5,t6,t7,t8
  279. # define P9(t1,t2,t3,t4,t5,t6,t7,t8,t9) t1,t2,t3,t4,t5,t6,t7,t8,t9
  280. # define P10(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10) t1,t2,t3,t4,t5,t6,t7,t8,t9,t10
  281. # define P11(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11) t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11
  282. # define P12(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12) t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12
  283. #else
  284. # define P0() /* */
  285. # define P1(t1) /* */
  286. # define P2(t1,t2) /* */
  287. # define P3(t1,t2,t3) /* */
  288. # define P4(t1,t2,t3,t4) /* */
  289. # define P5(t1,t2,t3,t4,t5) /* */
  290. # define P6(t1,t2,t3,t4,t5,t6) /* */
  291. # define P7(t1,t2,t3,t4,t5,t6,t7) /* */
  292. # define P8(t1,t2,t3,t4,t5,t6,t7,t8) /* */
  293. # define P9(t1,t2,t3,t4,t5,t6,t7,t8,t9) /* */
  294. # define P10(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10) /* */
  295. # define P11(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11) /* */
  296. # define P12(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12) /* */
  297. #endif
  298.  
  299. /* Standard error printing macros. */
  300. /* Use dprintf for messages that just go to dstderr, */
  301. /* eprintf for error messages to estderr that include the program name, */
  302. /* lprintf for debugging messages that should include line number info. */
  303. /* Since we intercept fprintf to redirect output under MS Windows, */
  304. /* we have to define dputc and dputs in terms of fprintf also. */
  305.  
  306. /* dstderr and estderr may be redefined. */
  307. #define dstderr stderr
  308. #define estderr stderr
  309.  
  310. #define dputc(chr) dprintf1("%c", chr)
  311. #define dputs(str) dprintf1("%s", str)
  312. #define dprintf(str)\
  313.   fprintf(dstderr, str)
  314. #define dprintf1(str,arg1)\
  315.   fprintf(dstderr, str, arg1)
  316. #define dprintf2(str,arg1,arg2)\
  317.   fprintf(dstderr, str, arg1, arg2)
  318. #define dprintf3(str,arg1,arg2,arg3)\
  319.   fprintf(dstderr, str, arg1, arg2, arg3)
  320. #define dprintf4(str,arg1,arg2,arg3,arg4)\
  321.   fprintf(dstderr, str, arg1, arg2, arg3, arg4)
  322. #define dprintf5(str,arg1,arg2,arg3,arg4,arg5)\
  323.   fprintf(dstderr, str, arg1, arg2, arg3, arg4, arg5)
  324. #define dprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
  325.   fprintf(dstderr, str, arg1, arg2, arg3, arg4, arg5, arg6)
  326. #define dprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
  327.   fprintf(dstderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
  328. #define dprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
  329.   fprintf(dstderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  330. #define dprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
  331.   fprintf(dstderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
  332. #define dprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
  333.   fprintf(dstderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
  334. #define dprintf11(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11)\
  335.   fprintf(dstderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11)
  336. #define dprintf12(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12)\
  337.   fprintf(dstderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12)
  338.  
  339. #ifdef PROGRAM_NAME
  340. extern const char *PROGRAM_NAME;
  341. #  define _epn fprintf(estderr, "%s: ", PROGRAM_NAME),
  342. #else
  343. #  define _epn /* */
  344. #endif
  345.  
  346. #define eprintf(str)\
  347.   (_epn fprintf(estderr, str))
  348. #define eprintf1(str,arg1)\
  349.   (_epn fprintf(estderr, str, arg1))
  350. #define eprintf2(str,arg1,arg2)\
  351.   (_epn fprintf(estderr, str, arg1, arg2))
  352. #define eprintf3(str,arg1,arg2,arg3)\
  353.   (_epn fprintf(estderr, str, arg1, arg2, arg3))
  354. #define eprintf4(str,arg1,arg2,arg3,arg4)\
  355.   (_epn fprintf(estderr, str, arg1, arg2, arg3, arg4))
  356. #define eprintf5(str,arg1,arg2,arg3,arg4,arg5)\
  357.   (_epn fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5))
  358. #define eprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
  359.   (_epn fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6))
  360. #define eprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
  361.   (_epn fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
  362. #define eprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
  363.   (_epn fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
  364. #define eprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
  365.   (_epn fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
  366. #define eprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
  367.   (_epn fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10))
  368.  
  369. #if __LINE__                /* compiler provides it */
  370. #  define _epl _epn fprintf(estderr, "%s(%d): ", __FILE__, __LINE__),
  371. #else
  372. #  define _epl _epn
  373. #endif
  374.  
  375. #define lprintf(str)\
  376.   (_epl fprintf(estderr, str))
  377. #define lprintf1(str,arg1)\
  378.   (_epl fprintf(estderr, str, arg1))
  379. #define lprintf2(str,arg1,arg2)\
  380.   (_epl fprintf(estderr, str, arg1, arg2))
  381. #define lprintf3(str,arg1,arg2,arg3)\
  382.   (_epl fprintf(estderr, str, arg1, arg2, arg3))
  383. #define lprintf4(str,arg1,arg2,arg3,arg4)\
  384.   (_epl fprintf(estderr, str, arg1, arg2, arg3, arg4))
  385. #define lprintf5(str,arg1,arg2,arg3,arg4,arg5)\
  386.   (_epl fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5))
  387. #define lprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
  388.   (_epl fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6))
  389. #define lprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
  390.   (_epl fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
  391. #define lprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
  392.   (_epl fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
  393. #define lprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
  394.   (_epl fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
  395. #define lprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
  396.   (_epl fprintf(estderr, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10))
  397.  
  398. #endif                    /* std_INCLUDED */
  399.